<?php

namespace App\Admin\Actions\ShopProduct;

use App\Library\Logic\OperateLogLogic;
use App\Models\ShopProductModel;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Actions\Response;
use Encore\Admin\Facades\Admin;

class ShopProductRecommendAction extends RowAction
{
    public function __construct(int $status = 0)
    {
        parent::__construct();
        $this->name = $status === 1 ? '取消推荐' : '推荐';
    }

    public function handle(ShopProductModel $model): Response
    {
        if (is_null($model)) return $this->response()->error(__('errorCode.433'))->refresh();
        else if ((int)$model->product_status !== 2) return $this->response()->error(__('errorCode.642'))->refresh();
        $model->status_recommend = (int)$model->status_recommend ? 0 : 1;
        $model->save();
        OperateLogLogic::add(Admin::user()->user_id, 'admin.orderReturn.recommend', ['statusRecommend' => (int)$model->status_recommend ? 0 : 1]);
        return $this->response()->success('操作成功')->refresh();
    }

    /**
     * dialog
     * @return void
     */
    public function dialog(): void
    {
        $this->confirm('确定操作吗?');
    }
}
